package com.ch01.ex21;

import java.util.Date;
import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * Created by zc on 2016/1/27.
 */
public class SearchTesk implements Runnable {
    private Resut resut;

    public SearchTesk(Resut resut) {
        this.resut = resut;
    }

    @Override
    public void run() {
        String name = Thread.currentThread().getName();
        System.out.printf("Thread %s: Start\n", name);

        try {
            doTask();
            resut.setName(name);
        } catch (InterruptedException e) {
            System.out.printf("Thread %s: Interrupted\n", name);
//            e.printStackTrace();
            return;
        }

        System.out.printf("Thread %s: End\n", name);

    }

    private void doTask() throws InterruptedException {
        Random random = new Random(new Date().getTime());
        int value = (int)(random.nextDouble()*100);
        System.out.printf("Thread %s: %d\n",
                Thread.currentThread().getName(), value);
        TimeUnit.SECONDS.sleep(value);
    }
}
